package com.mall.order.services;

import com.mall.order.OrderPromoService;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.handler.InitOrderHandler;
import com.mall.order.biz.handler.LogisticalHandler;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.CartProductDto;
import com.mall.order.dto.CreateSeckillOrderRequest;
import com.mall.order.dto.CreateSeckillOrderResponse;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.ProductDetailDto;
import com.mall.shopping.dto.ProductDetailRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class OrderPromoServiceImpl implements OrderPromoService {

    @Reference(check = false,timeout = 3000)
    IProductService iProductService;
    @Autowired
    InitOrderHandler initOrderHandler;
    @Autowired
    LogisticalHandler logisticalHandler;
    @Override
    public CreateSeckillOrderResponse createPromoOrder(CreateSeckillOrderRequest request) {
        //秒杀库存已经扣减
        CreateSeckillOrderResponse response = new CreateSeckillOrderResponse();
        CreateOrderContext createOrderContext = new CreateOrderContext();
        BeanUtils.copyProperties(request,createOrderContext);
        //塞商品id数据
        List<Long> longs = new ArrayList<>();
        longs.add(request.getProductId());
        createOrderContext.setBuyProductIds(longs);
        //塞购物车商品数据
        List<CartProductDto> cartProductDtos = new ArrayList<>();
        CartProductDto cartProductDto = new CartProductDto();
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(request.getProductId());
        ProductDetailDto productDetailDto = iProductService.getProductDetail(productDetailRequest).getProductDetailDto();
        BeanUtils.copyProperties(productDetailDto,cartProductDto);
        cartProductDto.setProductNum(1L);
        cartProductDto.setLimitNum(1L);
        cartProductDto.setSalePrice(request.getOrderTotal());
        cartProductDto.setProductImg(productDetailDto.getProductImageBig());
        cartProductDtos.add(cartProductDto);
        createOrderContext.setCartProductDtoList(cartProductDtos);
        try {
            //创建订单和物流信息
            initOrderHandler.handle(createOrderContext);
            logisticalHandler.handle(createOrderContext);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());

        }catch (Exception e){
            response.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }
}
